import requests
import parsel
import pandas
import re


def sellerdefense_():
    url = 'https://sellerdefense.cn/brands/'
    headers = {

        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': 'PHPSESSID=5dhk8thpu3nhlimhugv5e8stk2',
        'Host': 'sellerdefense.cn',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',

    }
    response = requests.get(url, headers=headers).content.decode()
    # print(response)
    response_parsel = parsel.Selector(response)
    response_parsel_all = response_parsel.css('.list-brand')
    # print(response_parsel_all)
    # df1=pandas.DataFrame()
    list1 = []
    for i in response_parsel_all:
        if i.css('a::attr(href)').get() != '':
            url = i.css('a::attr(href)').get()
            print(i.css('a::attr(href)').get())
            sellerdefense_1(url, list1)
    return list1
    # df1.append( sellerdefense_1(url))
    # break
    # df1.to_excel('jim.xlsx')
    # # df1.save()


def sellerdefense_1(url, list1):
    headers = {

        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': 'PHPSESSID=d57ug4d0ojrlv96gtbnfv9qoc2; pvc_visits[0]=1646296862b36922',
        'Host': 'sellerdefense.cn',
        'Referer': 'https://sellerdefense.cn/brands/',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',

    }
    # url = 'https://sellerdefense.cn/hugo-boss/'
    response_ = requests.get(url=url, headers=headers).content.decode()
    # print(response_)
    response_re = re.findall('<p>(.*)</p>', response_)
    # print(response_re)
    response_parsel = parsel.Selector(response_)
    title = response_parsel.css('.page-title-head h1::text').extract_first()
    all_str = ''
    for data in response_re:

        if '代理律所' in data and '起诉类型' in data:
            all_str = data.replace('<br>', '').replace('<strong>', '').replace('</strong>', '')


        else:
            list2 = ['品牌方','起诉类型','代理律所','品牌公司','侵权类型','原告公司','维权类型']
            # if '品牌方' in data or '起诉类型' in data or '代理律所' in data:
            for i in list2:
                if i in data:
                    all_str +=data.replace('<br>', '').replace('<strong>', '').replace('</strong>', '')
    print(all_str)
    response_parsel = parsel.Selector(response_)
    all_1 = response_parsel.css('.wp-block-table tbody:nth-child(1) > tr:nth-child(2)')
    # print(all_1)
    if all_1 != []:
        Prosecution_time = all_1.css('td:nth-child(1)::text').get()  # 起诉时间
        Case_number = all_1.css('td:nth-child(2)::text').get()  # 案件号
        Law_firm = all_1.css('td:nth-child(3)::text').get()  # 律所
        brand = all_1.css('td:nth-child(4)::text').get()  # 品牌
        Brand_side_ = all_1.css('td:nth-child(5)::text').get()  # 品牌方
        print('22222222',Prosecution_time, Case_number, Law_firm, brand, Brand_side_)

        list1.append([url,title,all_str, Prosecution_time, Case_number, Law_firm, brand, Brand_side_])

    else:
        Prosecution_time = ''  # 起诉时间
        Case_number = ''  # 案件号
        Law_firm = ''  # 律所
        brand = ''  # 品牌
        Brand_side_ = ''  # 品牌方
        print('3333333',Prosecution_time, Case_number, Law_firm, brand, Brand_side_)

        list1.append([url,title,all_str, Prosecution_time, Case_number, Law_firm, brand, Brand_side_])

    # response_parsel = parsel.Selector(response_)
    # title = response_parsel.css('.page-title-head h1::text').extract_first()
    # print(title)
    # all = response_parsel.css('div div p::text').getall()
    # print(all)
    # alll = response_parsel.xpath('//*[@id="post-36922"]/div[1]/p[2]')
    # //*[@id="content"]/p[1]/strong[1]
    # # brand_side = all[0]  # 品牌方
    # # print(brand_side)
    # # Type_of_prosecution = all[1]  # 起诉类型
    # # print(Type_of_prosecution)
    # # Agency_law_firm = all[2]  # 代理律师
    # # print(Agency_law_firm)
    # all_1 = response_parsel.css('.wp-block-table tbody:nth-child(1) > tr:nth-child(2) td::text').getall()
    # print(all_1)
    # Prosecution_time = all_1[0]  # 起诉时间
    # Case_number = all_1[1]  # 案件号
    # Law_firm = all_1[2]  # 律所
    # brand = all_1[3]  # 品牌
    # Brand_side_ = all_1[4]  # 品牌方
    # print(Prosecution_time, Case_number, Law_firm, brand, Brand_side_)
    # dicts = {'品牌方1':[brand_side],'起诉类型':[Type_of_prosecution],'代理律师':[Agency_law_firm],'起诉时间':[Prosecution_time],'案件号':[Case_number],'律所':[Law_firm],'品牌':[brand],'品牌方2':[Brand_side_]}
    # df = pandas.DataFrame(dicts)
    # df.to_excel('jim.xlsx')
    # return df


def pandas_excel():
    df = pandas.DataFrame(sellerdefense_(), columns=['链接','标题','品牌综合', '起诉时间', '案件号', '律所',
                                                     '品牌', '品牌方2'])
    df.to_excel('jim1.xlsx', index=None)


if __name__ == '__main__':
    pandas_excel()
